﻿namespace Searches
{
    public static class StringOperations
    {
        public static string SwitchWordPairs(this string s)
        {
            string result = string.Empty;

            string word1 = string.Empty;
            string word2 = string.Empty;
            string buffer = string.Empty;

            for (int i = 0; i <= s.Length; i++)
            {
                if (word1.Length > 0 && word2.Length > 0 || i == s.Length)
                {
                    result += word2 + buffer + word1;
                    if (i == s.Length)
                        break;
                    word1 = word2 = buffer = string.Empty;
                }

                if (char.IsLetter(s[i]))
                {
                    if (word1.Length == 0)
                    {
                        while (i < s.Length && char.IsLetter(s[i]))
                        {
                            word1 += s[i++];
                        }
                    }
                    else
                    {
                        while (i < s.Length && char.IsLetter(s[i]))
                        {
                            word2 += s[i++];
                        }
                    }

                    i--;
                    continue;
                }

                if (word1.Length > 0)
                {
                    buffer += s[i];
                }
                else
                {
                    result += s[i];
                }
            }

            return result;
        }

        public static int IndexOfSubstring(this string s, string substring)
        {
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == substring[0])
                {
                    int j;
                    for (j = i; j < s.Length && j - i < substring.Length; j++)
                    {
                        if(s[j] != substring[j-i])
                        {
                            break;
                        }
                    }

                    if(j-i==substring.Length)
                    {
                        return i;
                    }
                }
            }

            return -1;
        }

        public static string ChangeWordOrder(this string s)
        {
            string result = string.Empty;
            string currentWord = string.Empty;

            for (int i = s.Length - 1; i >= 0; i--)
            {
                while (i>=0 && char.IsLetter(s[i]))
                {
                    currentWord += s[i--];
                }

                if(currentWord.Length > 0)
                {
                    result += currentWord.Reverse();
                    currentWord = string.Empty;
                    i++;
                    continue;
                }

                result += s[i];
            }

            return result;
        }

        private static string Reverse(this string s)
        {
            string result = string.Empty;

            for (int i = s.Length - 1; i >= 0; i--)
            {
                result += s[i];
            }

            return result;
        }
    }
}